﻿                   using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Smith.ImageConverter
{
    public class BaseRotation : IRotation
    {
        public ResampleOption ResampleMode
        {
            get;
            set;
        }

        public RotationAngle RotationAngle
        {
            get;
            set;
        }

        public int Angel
        {
            get { return (int)RotationAngle; }
        }

        public virtual Bitmap Rotate(Bitmap image)
        {
            if (CanDo(image))
            {
                image.RotateFlip(GetRotateFlipType());
                Bitmap result = new Bitmap(image.Width, image.Height);
                using (Graphics g = Graphics.FromImage(result))
                {
                    g.InterpolationMode = InterpolationMode.Default;
                    g.DrawImage(image, 0, 0, result.Width, result.Height);
                    return result;
                }
            }
            else
            {
                return image;
            }
        }

        protected virtual bool CanDo(Bitmap image)
        {
            return false;
        }

        protected RotateFlipType GetRotateFlipType()
        {
            switch (this.RotationAngle)
            {
                case RotationAngle.Left90:
                    return RotateFlipType.Rotate270FlipNone;
                case RotationAngle.Left180:
                    return RotateFlipType.Rotate180FlipNone;
                case RotationAngle.Left270:
                    return RotateFlipType.Rotate90FlipNone;
                case RotationAngle.Right90:
                    return RotateFlipType.Rotate90FlipNone;
                case RotationAngle.Right180:
                    return RotateFlipType.Rotate180FlipNone;
                case RotationAngle.Right270:
                    return RotateFlipType.Rotate270FlipNone;
                default:
                    return RotateFlipType.RotateNoneFlipNone;
            }
        }
    }
}
